package system.logic.gui.process;

import java.awt.BorderLayout;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

import system.logger.ClusterLogger;
import system.process.ProcessBo;

import system.db.BaseDb;

public class ProcessFrame extends JPanel 
{
    private final static ClusterLogger log = ClusterLogger.getLogger(ProcessFrame.class.getName());
    
    JTable tbl = null;
    
    Vector<Vector<String>> tblData = null;
    
    ProcessTableModel myTableModel = null;
    
	public ProcessFrame() 
	{
	    myTableModel = new ProcessTableModel();
        tbl = new JTable(myTableModel);
        tbl.createDefaultColumnsFromModel();
        
	    this.setLayout(new BorderLayout());
	    this.add(tbl.getTableHeader(), BorderLayout.NORTH);
	    this.add(tbl, BorderLayout.CENTER);
	    this.setVisible(true);
	    
	    doRefresh();
	}
	
	public void doRefresh()
	{
	    try
        {
	        tblData = new Vector<Vector<String>>();
	        
            ResultSet rs = BaseDb.getInstance().dbQuery("SELECT * FROM " + ProcessBo.TB_NAME);
            
            while( rs.next() )
            {
                Vector<String> data = new Vector<String>();
                
                data.addElement(rs.getString(1));
                data.addElement(rs.getString(2));
                data.addElement(rs.getString(3));
                data.addElement(rs.getString(4));
                
                tblData.add(data);
            }
            rs.close();
        }
        catch(Exception e)
        {
            log.error("Could not read from table 'process'");
            log.error(e.getMessage());
            e.printStackTrace();
        }
        
        myTableModel.fireTableDataChanged();
	}
	
	/**
     * close complete frame
     */
    public void doClose()
    {
        // TODO
        System.exit(0);
    }
    
	class ProcessTableModel extends AbstractTableModel
	{
        @Override
        public Class<?> getColumnClass(int columnIndex)
        {
            return String.class;
        }

        @Override
        public int getColumnCount( )
        {
            return 4;
        }

        @Override
        public String getColumnName(int columnIndex)
        {
            String rt = "<empty>";
            
            switch(columnIndex)
            {
                case 0:
                    rt = "Process Name";
                    break;
                case 1:
                    rt = "Process State";
                    break;
                case 2:
                    rt = "Comment";
                    break;
                case 3:
                    rt = "Type";
                    break;
            }
            
            return rt;
        }

        @Override
        public int getRowCount( )
        {
            if(tblData != null)
                return tblData.size();
            else
                return 0;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex)
        {
            if(tblData != null)
            {
                if(columnIndex == 3)
                {
                    if(tblData.get(rowIndex).get(columnIndex).equals("" + ProcessBo.TYPE_SYSTEM))
                        return "SYSTEM";
                    else
                        return "USER";
                }
                
                if(columnIndex == 1)
                {
                    if(tblData.get(rowIndex).get(columnIndex).equals("" + ProcessBo.STATE_ERROR))
                        return "ERROR";
                    else if(tblData.get(rowIndex).get(columnIndex).equals("" + ProcessBo.STATE_PAUSED))
                        return "PAUSED";
                    else if(tblData.get(rowIndex).get(columnIndex).equals("" + ProcessBo.STATE_RUNNING))
                        return "RUNNING";
                    else if(tblData.get(rowIndex).get(columnIndex).equals("" + ProcessBo.STATE_TERMINATED))
                        return "TERMINATED";
                }

                return tblData.get(rowIndex).get(columnIndex);
            }
            else
                return "<error>";
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex)
        {
            return false;
        }
	}
}
